package web.fabrimind.sm.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import web.fabrimind.bdm.domain.ProductionLine;
import web.fabrimind.common.annotation.Log;
import web.fabrimind.common.core.controller.BaseController;
import web.fabrimind.common.core.domain.AjaxResult;
import web.fabrimind.common.enums.BusinessType;
import web.fabrimind.sm.domain.TeamMember;
import web.fabrimind.sm.service.ITeamMemberService;
import web.fabrimind.common.utils.poi.ExcelUtil;
import web.fabrimind.common.core.page.TableDataInfo;

/**
 * 团队成员Controller
 * 
 * @author zhihui
 * @date 2025-03-30
 */
@RestController
@RequestMapping("/sm/member")
public class TeamMemberController extends BaseController
{
    @Autowired
    private ITeamMemberService teamMemberService;

    /**
     * 查询团队成员列表
     */
    @PreAuthorize("@ss.hasPermi('sm:member:list')")
    @GetMapping("/list")
    public TableDataInfo list(TeamMember teamMember)
    {
        startPage();
        List<TeamMember> list = teamMemberService.selectTeamMemberList(teamMember);
        return getDataTable(list);
    }

    /**
     * 查询可用成员列表（包括所有用户和已有团队成员）
     */
    @PreAuthorize("@ss.hasPermi('sm:member:list')")
    @GetMapping("/available")
    public TableDataInfo available(TeamMember teamMember)
    {
        startPage();
        List<TeamMember> list = teamMemberService.selectAvailableMemberList(teamMember);
        return getDataTable(list);
    }

    /**
     * 导出团队成员列表
     */
    @PreAuthorize("@ss.hasPermi('sm:member:export')")
    @Log(title = "团队成员", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TeamMember teamMember)
    {
        List<TeamMember> list = teamMemberService.selectTeamMemberList(teamMember);
        ExcelUtil<TeamMember> util = new ExcelUtil<TeamMember>(TeamMember.class);
        util.exportExcel(response, list, "团队成员数据" +
                "");
    }

    /**
     * 获取团队成员详细信息
     * 是
     */
    @PreAuthorize("@ss.hasPermi('sm:member:query')")
    @GetMapping(value = "/{memberId}")
    public AjaxResult getInfo(@PathVariable("memberId") Long memberId)
    {
        return success(teamMemberService.selectTeamMemberByMemberId(memberId));
    }

    /**
     * 新增团队成员
     */
    @PreAuthorize("@ss.hasPermi('sm:member:add')")
    @Log(title = "团队成员", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TeamMember teamMember)
    {
        return toAjax(teamMemberService.insertTeamMember(teamMember));
    }

    /**
     * 修改团队成员
     */
    @PreAuthorize("@ss.hasPermi('sm:member:edit')")
    @Log(title = "团队成员", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TeamMember teamMember)
    {
        return toAjax(teamMemberService.updateTeamMember(teamMember));
    }

    /**
     * 删除团队成员
     */
    @PreAuthorize("@ss.hasPermi('sm:member:remove')")
    @Log(title = "团队成员", businessType = BusinessType.DELETE)
	@DeleteMapping("/{memberIds}")
    public AjaxResult remove(@PathVariable Long[] memberIds)
    {
        return toAjax(teamMemberService.deleteTeamMemberByMemberIds(memberIds));
    }

    /**
     * 查询所属客户列表（不分页）
     */
    @PreAuthorize("@ss.hasPermi('sm:user:list')")
    @GetMapping("/user/getlist")
    public AjaxResult getUserList(TeamMember teamMember) {
        List<TeamMember> userList = teamMemberService.selectUserList(teamMember);
        return AjaxResult.success(userList);
    }


    /**
     * 查询所属团队列表（不分页）
     */
    @PreAuthorize("@ss.hasPermi('sm:team:list')")
    @GetMapping("/team/getlist")
    public AjaxResult getTeamList(TeamMember teamMember) {
        List<TeamMember> teamList = teamMemberService.selectTeamList(teamMember);
        return AjaxResult.success(teamList);
    }


}
